/*
 * wueasy - A Java Distributed Rapid Development Platform.
 * Copyright (C) 2017-2019 wueasy.com

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.

 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.wueasy.admin.task;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.wueasy.base.config.ZKConfig;
import com.wueasy.base.util.SpringHelper;


/**
 * 基础配置中心
 * @author: fallsea
 * @version 1.0
 */
@Configuration
public class WueasyTaskConfig {
	
	@Bean(name = "regCenter")
    public CoordinatorRegistryCenter createRegistryCenter() {
		
		ZKConfig zkConfig=SpringHelper.getBean("wueasyZkConfig", ZKConfig.class);
		ZookeeperConfiguration zookeeperConfiguration = null;
        if(zkConfig != null)
        {
        	zookeeperConfiguration = new ZookeeperConfiguration(zkConfig.getServers(), "wueasy-task");
        	zookeeperConfiguration.setSessionTimeoutMilliseconds(zkConfig.getSessionTimeout());
        	zookeeperConfiguration.setConnectionTimeoutMilliseconds(zkConfig.getConnectionTimeout());
        }
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
        regCenter.init();
        return regCenter;
    }
	
}
